package com.example.desksginktapp.views

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.baselibrary.utlis.LogUtils
import com.example.baselibrary.utlis.setOnSingleClickListener
import com.example.desksginktapp.R
import com.example.desksginktapp.adapter.VisitorAdapter
import com.example.desksginktapp.databinding.ActivityVisitorySearchBinding
import com.example.desksginktapp.viewmodels.VisitorySearchViewModel
import com.example.httplibrary.beans.VisitorDataListResultBean
import com.xuexiang.xui.utils.XToastUtils
import com.zyf.baselibrary.base.BaseViewBindingActivity

class VisitorySearchActivity :
    BaseViewBindingActivity<ActivityVisitorySearchBinding, VisitorySearchViewModel>() {
    private lateinit var visitorAdapter: VisitorAdapter
    override fun provideViewModel(): VisitorySearchViewModel {
        return VisitorySearchViewModel()
    }

    override fun initViews() {
        super.initViews()
        binding.etSerachPhone.setText("362334199106173116")
        // 初始化 RecyclerView 和 Adapter
        initRecyclerView()
        binding.llConfig.setOnSingleClickListener {
            //去判断数据是否为空
            if (binding.etSerachPhone.text.isNullOrBlank()) {
                XToastUtils.error("请输入查询证件号")
                return@setOnSingleClickListener
            } else {
                viewModel.visitorDataList(binding.etSerachPhone.text.toString())
            }

        }

        binding.pgFlipper.setOnPageChangeListener { position ->
            // 切换 RecyclerView 显示的页面
            LogUtils.info("切换 RecyclerView 显示的页面 $position")
        }
    }

    override fun initData() {
        super.initData()
        viewModel.visitorData.observe(this) {
            LogUtils.info("获取到的数据 $it")
            //把这个数据添加到适配器中
            // 更新适配器数据
            visitorAdapter.updateData(it.records)
        }
    }

    private fun initRecyclerView() {
        val visitorList = mutableListOf<VisitorDataListResultBean.ResultDTO.RecordsDTO>()
        // 创建并设置 Adapter
        visitorAdapter = VisitorAdapter(visitorList)
        binding.rcVisitorList.layoutManager = LinearLayoutManager(this)
        binding.rcVisitorList.adapter = visitorAdapter

        // 设置操作按钮点击监听器
        visitorAdapter.setOnItemClickListener(object : VisitorAdapter.OnItemClickListener {
            override fun onItemClick(
                visitor: VisitorDataListResultBean.ResultDTO.RecordsDTO,
                position: Int
            ) {
                handleVisitorOperation(visitor, position)
            }
        })
    }
    private fun handleVisitorOperation(
        visitor: VisitorDataListResultBean.ResultDTO.RecordsDTO,
        position: Int
    ) {
        // 在这里处理操作按钮的点击事件
        // 例如：签到、签离、查看详情等操作
        XToastUtils.info("点击了操作按钮: ${visitor.visitorName}")

        // 根据访客状态执行不同操作
        when (visitor.visitStatus) {
            "未签到" -> {
                // 处理签到逻辑
                XToastUtils.info("为 ${visitor.visitorName} 办理签到")
            }
            "已签到" -> {
                // 处理签离逻辑
                XToastUtils.info("为 ${visitor.visitorName} 办理签离")
            }
            else -> {
                // 其他操作
                XToastUtils.info("查看访客详情")
            }
        }
    }

}